home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / clgraph1.zip / CL_LIST.CPP < prev    next >
C/C++ Source or Header  |  1993-09-01  |  5KB  |  268 lines

  1. #ifndef boolean
  2.   typedef char boolean;
  3. #endif
  4.  
  5. template <class DataType>
  6. class DataSource
  7. {
  8. friend DataIterator<DataType>;
  9.  
  10. protected:
  11.   long        capacity;
  12.   long        size;
  13.  
  14. public:
  15.               DataSource( long maxSize = 0xFFFFFFF ) {
  16.                 capacity = maxSize;
  17.                 size = 0;
  18.               };
  19.   virtual     ~DataSource() {};
  20.   virtual
  21.   boolean     Insert( DataType data ) = 0;
  22.   virtual
  23.   boolean     Delete( long cell ) = 0;
  24.   virtual
  25.   DataType    operator[]( long cell ) = 0;
  26.   long        Size()               { return( size ); };
  27.   boolean     Empty()              { return( size == 0 ); };
  28.   boolean     Full()               { return( size == capacity ); };
  29. };
  30.       
  31.  
  32. template <class DataType>
  33. class DataIterator
  34. {
  35.   DataSource<DataType>& container;
  36.   long        current; 
  37. public:
  38.               DataIterator( void *contAdd );
  39.   long        Position();
  40.   boolean     First();
  41.   boolean     Next();
  42.   boolean     Previous();
  43.   boolean     Last();
  44.   boolean     End();
  45.   boolean     Empty();
  46.   boolean     Goto( long cell );
  47.   DataType    Get();
  48. };
  49.  
  50.  
  51. template <class DataType>
  52. DataIterator<DataType>::DataIterator( void *contAdd ) :
  53.               container(*(DataSource<DataType> *)contAdd)
  54. {
  55.  current = 0;
  56. }
  57.  
  58.  
  59. template <class DataType>
  60. long DataIterator<DataType>::Position()
  61. {                             
  62.   return( current );
  63. }
  64.  
  65.  
  66. template <class DataType>
  67. boolean DataIterator<DataType>::First()
  68. {
  69.   current = 0;
  70.   if ( container.Empty() ) {
  71.     return( FALSE );
  72.   }
  73.   return( TRUE );
  74.  }
  75.  
  76.  
  77. template <class DataType>
  78. boolean DataIterator<DataType>::Last()
  79. {
  80.   current = container.Size();
  81.   if ( container.Empty() ) {
  82.     return( FALSE );
  83.   }
  84.   return( TRUE );
  85. }
  86.  
  87.  
  88. template <class DataType>
  89. boolean DataIterator<DataType>::Next()
  90. {
  91.   if ( current < (container.Size() - 1) ) {
  92.     current++;
  93.     return( TRUE );
  94.   }
  95.   return( FALSE );
  96. }
  97.  
  98.  
  99. template <class DataType>
  100. boolean DataIterator<DataType>::Previous()
  101. {
  102.   if ( current > 0 ) {
  103.     current--;
  104.     return( TRUE );
  105.   }
  106.   return( FALSE );
  107. }
  108.  
  109.  
  110. template <class DataType>
  111. boolean DataIterator<DataType>::Goto( long cell )
  112. {
  113.   if ( ! container.Empty() ) {
  114.     if ( (cell >= 0) && (cell < container.Size()) ) {
  115.       current = cell;
  116.       return( TRUE );
  117.     }
  118.   }
  119.   return( FALSE );
  120. }
  121.  
  122.  
  123. template <class DataType>
  124. boolean DataIterator<DataType>::End()
  125. {
  126.   return( current == container.Size() );
  127. }
  128.  
  129.  
  130. template <class DataType>
  131. boolean DataIterator<DataType>::Empty()
  132. {
  133.   return( container.Empty() );
  134. }
  135.  
  136.  
  137. template <class DataType>
  138. DataType DataIterator<DataType>::Get()
  139. {
  140.   return( container[current] );
  141. }
  142.  
  143.  
  144. #ifdef __BORLANDC__
  145.   #pragma warn -inl
  146. #endif
  147. template <class DataType>
  148. class LinkedList : public DataSource<DataType>
  149. {
  150. private:
  151.   class Node
  152.   {
  153.     public:
  154.     DataType  data;
  155.     Node      *previous;
  156.     Node      *next;
  157.               Node( DataType d ) : data(d) {};
  158.   };
  159.  
  160.   Node        *head;
  161.   Node        *tail;
  162.   Node        *Goto( long cell ) {
  163.                  Node *n;
  164.  
  165.                  if ( head == NULL ) {
  166.                    return( NULL );
  167.                  } else {
  168.                    n = head;
  169.                    for ( n = head; cell > 0; cell-- ) {
  170.                      n = n->next;
  171.                      if ( n == NULL ) {
  172.                        return( NULL );
  173.                      }
  174.                    }                                
  175.                  }
  176.                  return( n );
  177.               };
  178.  
  179. public:                                    
  180.               LinkedList( long maxSize = 0xFFFFFF );
  181.               ~LinkedList();
  182.   boolean     Insert( DataType data );
  183.   boolean     Delete( long cell );
  184.   DataType    operator[]( long cell );
  185. };
  186. #ifdef __BORLANDC__
  187.   #pragma warn .inl
  188. #endif
  189.  
  190.  
  191. template <class DataType>
  192. LinkedList<DataType>::LinkedList( long maxSize ) :
  193.                       DataSource<DataType>(maxSize)
  194. {
  195.   head = tail = NULL;
  196. }
  197.  
  198.  
  199. template <class DataType>
  200. LinkedList<DataType>::~LinkedList()
  201. {
  202.   for ( ; head != NULL; ) {
  203.     Node *temp;
  204.     temp = head;
  205.     head = head->next;
  206.     delete temp;
  207.   }
  208. }
  209.  
  210.  
  211. template <class DataType>
  212. boolean LinkedList<DataType>::Insert( DataType data )
  213. {
  214.   if ( size >= capacity ) {
  215.     return( FALSE );
  216.   } else {
  217.     if ( tail == NULL ) {
  218.       head = tail = new Node(data);
  219.       head->previous = NULL;
  220.     } else {
  221.       tail->next = new Node(data);
  222.       tail->next->previous = tail;
  223.       tail = tail->next;
  224.     }
  225.     size++;
  226.     return( TRUE );
  227.   }
  228. }
  229.  
  230.  
  231. template <class DataType>
  232. boolean LinkedList<DataType>::Delete( long cell )
  233. {
  234.   if ( (cell >= 0) && (cell <= size) ) {
  235.     Node *n = Goto( cell );
  236.     if ( n == head ) {
  237.       head = n->next;
  238.     } else {
  239.       n->previous->next = n->next;
  240.     }
  241.     if ( n == tail ) {
  242.       tail = n->previous;
  243.     } else {
  244.       n->next->previous = n->next;
  245.     }
  246.     delete n;
  247.     size--;
  248.     return( TRUE );
  249.   }
  250.   return( FALSE );
  251. }
  252.  
  253.  
  254. template <class DataType>
  255. DataType LinkedList<DataType>::operator[]( long cell )
  256. {
  257.   static char buffer[sizeof(DataType)];
  258.  
  259.   Node *n = Goto( cell );
  260.  
  261.   if ( n != NULL ) {
  262.     return( n->data );
  263.   }
  264.   return( *(DataType *)buffer );
  265. }
  266.  
  267.  
  268.